
package com.ukao.steward.pesenter.valetRunners;

import com.ukao.steward.base.BasePresenter;
import com.ukao.steward.bean.MercCityBean;
import com.ukao.steward.bean.OrderInfoBean;
import com.ukao.steward.bean.ReceiptBean;
import com.ukao.steward.bean.StringBean;
import com.ukao.steward.bean.TradeOrderBean;
import com.ukao.steward.consts.Constant;
import com.ukao.steward.consts.SaveParams;
import com.ukao.steward.retrofit.ApiCallback;
import com.ukao.steward.util.CheckUtils;
import com.ukao.steward.util.DecimalUtil;
import com.ukao.steward.util.L;
import com.ukao.steward.util.T;
import com.ukao.steward.view.valetRunners.CreateOrderView;

import org.json.JSONArray;

import java.util.HashMap;

import rx.Observable;

/**
 * Created by ukaodev on 2017/6/28.
 */

public class CreateOrderPesenter extends BasePresenter<CreateOrderView> {


    public CreateOrderPesenter(CreateOrderView view) {
        attachView(view);
    }


    /**
     * 取送费
     */
    public void cityCarriage(   ) {
        mvpView.showLoading();
        HashMap mHashMap = new HashMap<String, String>();
        mHashMap.put("accessToken", SaveParams.getToken());
        L.i("mHashMap="+mHashMap.toString());
        addSubscription( apiStores.cityCarriage(Constant.createParameter(mHashMap)),
                new ApiCallback<MercCityBean>() {
                    @Override
                    public void onSuccess(MercCityBean model) {

                        if (model.getHttpCode() == 200) {

                            int carriage = model.getData().getTakeCarriage() + model.getData().getSendCarriage();

                            mvpView.carriageSucceed(model.getData(), CheckUtils.isEmptyNumber(carriage));
                        }else{

                            mvpView.showError(model.getMsg());
                        }

                    }


                    @Override
                    public void onFinish() {

                        mvpView.hideLoading();

                    }
                });

    }


    /**
     *   下单
     * @param mPriceDescArray   衣物列表
     * @param strbusiness   大类  tab
     * @param userId    用户ID
     * @param takeAddressId  地址Id
     * @param createCnt     数量
     * @param payablePrice   支付价格
     * @param isFreeCarriage  是否开启取送费
     * @param carriage    取送费
     * @param isStorageEnter                    1 订单入库
     */
    public void createOrderRuquest(JSONArray mPriceDescArray,
                                   String strbusiness, String userId, String takeAddressId, String createCnt, String payablePrice,boolean isFreeCarriage,String carriage,boolean isStorageEnter ) {
        HashMap mHashMap = new HashMap<String, String>();
        mHashMap.put("takeAddressId", takeAddressId);
        mHashMap.put("isFreeCarriage",isFreeCarriage?"0":"1");/*0是不免*/
        mHashMap.put("userId", userId);
        mHashMap.put("createCnt", createCnt); /*=购物车数量*/
        if (!CheckUtils.isNull(strbusiness)){        mHashMap.put("business", strbusiness);   }
        if (!CheckUtils.isNull(mPriceDescArray)){              mHashMap.put("proPriceDesc", String.valueOf(mPriceDescArray));   }
        mHashMap.put("payablePrice", DecimalUtil.multiply(payablePrice));/*购物车总价*/
        if (isStorageEnter){     mHashMap.put("stockIn", "1");  }
        mHashMap.put("accessToken", SaveParams.getToken());
        mvpView.showLoading();
        L.i("mHashMap=" + mHashMap.toString());
        Observable observable = null;
        observable = apiStores.createOrder(Constant.createParameter(mHashMap));
        addSubscription(observable,
                new ApiCallback<StringBean>() {
                    @Override
                    public void onSuccess(StringBean model) {
                        if (isStorageEnter){   mvpView.hideLoading(); }
                        if (model.getHttpCode() == 200) {
                            mvpView.createOrderSucceed(model.getData());


                        } else {

                            mvpView.showError(model.getMsg());
                        }

                    }


                    @Override
                    public void onFinish() {

                        mvpView.hideLoading();

                    }
                });
    }

    /**
     * 添加运单号
     *
     * @param orderId 订单号
     * @param transNo 运单号
     */
    public void addTransNo(String orderId,final String transNo,String userRemark) {

        HashMap mHashMap = new HashMap<String, String>();
        mHashMap.put("orderId", orderId);
        mHashMap.put("transNo", transNo);
        mHashMap.put("userRemark", userRemark);
        mHashMap.put("accessToken", SaveParams.getToken());
        L.i("mHashMap=" + mHashMap.toString());
        mvpView.showLoading();
        addSubscription(apiStores.transNo(Constant.createParameter(mHashMap)),
                new ApiCallback<StringBean>() {
                    @Override
                    public void onSuccess(StringBean model) {

                        if (model.getHttpCode() == 200) {
                    mvpView.addTransNoSucceed(transNo);
                        }
                        T.show(model.getMsg());
                    }


                    @Override
                    public void onFinish() {

                        mvpView.hideLoading();

                    }
                });

    }


    /**
     * 支付订单信息
     *
     * @param id
     */
    public void payInfo(String  id ) {
        HashMap mHashMap = new HashMap<String, String>();
        mHashMap.put("id",id);
        mHashMap.put("accessToken", SaveParams.getToken());
        L.i("mHashMap="+mHashMap.toString());
        mvpView.showLoading();
        addSubscription( apiStores.payInfo(Constant.createParameter(mHashMap)),
                new ApiCallback<OrderInfoBean>() {
                    @Override
                    public void onSuccess(OrderInfoBean model) {

                        if (model.getHttpCode() == 200) {
                            mvpView.OrderInfoSucceed(model.getData());


                        }else{

                            mvpView.showError(model.getMsg());
                        }

                    }


                    @Override
                    public void onFinish() {

                        mvpView.hideLoading();

                    }
                });

    }

    /**
     查询订单交易列表
     */
    public void tradeOrder( String  orderId  ) {
        mvpView.showLoading();
        HashMap mHashMap = new HashMap<String, String>();
        mHashMap.put("orderId",orderId);
        mHashMap.put("accessToken", SaveParams.getToken());
        addSubscription(apiStores.tradeOrder(Constant.createParameter(mHashMap)),
                new ApiCallback<TradeOrderBean>() {
                    @Override
                    public void onSuccess(TradeOrderBean model) {

                        if (model.getHttpCode() == 200) {
                            if (!CheckUtils.isEmpty(model.getData().getList())){
                                mvpView.loadtradeOrder(model.getData().getList());

                            }

                        }else{

                            mvpView.showError(model.getMsg());
                        }

                    }


                    @Override
                    public void onFinish() {

                        mvpView.hideLoading();

                    }
                });

    }


    /**
     *
     * 订单精准计价
     * @param orderId
     * @param createCnt  数量
     * @param mPriceDescArray  衣物数据
     * @param payablePrice     订单总价格
     * @param userRemark   备注
     * @param isFreeCarriage 是否需要取送费
     * @param carriage    取送费
     */
    public void valuationAccurate( String  orderId,String createCnt,JSONArray mPriceDescArray,String payablePrice,String  userRemark,  boolean isFreeCarriage,String carriage) {

        mvpView.showLoading();
        HashMap mHashMap = new HashMap<String, String>();
        mHashMap.put("id",orderId);
        mHashMap.put("isFreeCarriage",isFreeCarriage?"0":"1");/*0是不免*/
        if (!userRemark.isEmpty()){ mHashMap.put("userRemark",userRemark);    }
        mHashMap.put("createCnt",createCnt); /*=购物车数量*/
        if (!CheckUtils.isNull(mPriceDescArray)){              mHashMap.put("proPriceDesc", String.valueOf(mPriceDescArray));   }
        mHashMap.put("payablePrice", DecimalUtil.multiply(payablePrice));/*购物车总价*/
        mHashMap.put("accessToken", SaveParams.getToken());
        L.i("mHashMap="+mHashMap.toString());
        mvpView.showLoading();
        addSubscription(apiStores.valuationAccurate(Constant.createParameter(mHashMap)),
                new ApiCallback<StringBean>() {
                    @Override
                    public void onSuccess(StringBean model) {

                        if (model.getHttpCode() == 200) {
                            mvpView.valuationSucceed(model.getMsg());


                        }else{

                            mvpView.showError(model.getMsg());
                        }

                    }


                    @Override
                    public void onFinish() {

                        mvpView.hideLoading();

                    }
                });

    }


    /**
     *订单入库
     */
    public void orderStockIn( String  orderId,String  proPriceDesc ) {
        mvpView.showLoading();
        HashMap mHashMap=new HashMap<String,String>();
        mHashMap.put("orderId",orderId);
        mHashMap.put("proPriceDesc",proPriceDesc);
        mHashMap.put("accessToken", SaveParams.getToken());
        L.i("mHashMap"+mHashMap.toString());
        addSubscription(apiStores.orderStockIn(Constant.createParameter(mHashMap)),
                new ApiCallback<StringBean>() {
                    @Override
                    public void onSuccess(StringBean model) {
                        mvpView.hideLoading();
                        if (model.getHttpCode()==200){
                            mvpView.orderStockInSucceed();
                        }else{
                            mvpView.showError(model.getMsg());
                        }
                    }
                    @Override
                    public void onFinish() {
                        mvpView.hideLoading();
                    }
                });
    }
    /**
     * 查询订单号和运单号
     */
    public  void  queryBatchCode(int number){
        HashMap mHashMap=new HashMap<String,String>();
        mHashMap.put("batchCode ",number);

    }



    /**
     *打印订单
     */
    public void printReceipt( String  id ) {
        mvpView.showLoading();
        HashMap mHashMap=new HashMap<String,String>();
        mHashMap.put("orderId",id);
        mHashMap.put("accessToken", SaveParams.getToken());
        addSubscription(apiStores.printReceipt(Constant.createParameter(mHashMap)),
                new ApiCallback<ReceiptBean>() {
                    @Override
                    public void onSuccess(ReceiptBean model) {
                        mvpView.hideLoading();
                        if (model.getHttpCode()==200){
                            mvpView.loadReceipt(model.getData());
                        }else{
                            mvpView.showError(model.getMsg());
                        }


                    }



                    @Override
                    public void onFinish() {
                        mvpView.hideLoading();
                    }

                });
    }



}
